package UILag;
import UILag.*;
import ctrLag.*;
import modelLag.*;
import java.util.ArrayList;

/**
 * Order UI
 * 
 * @author (Christian, Umut, Christoffer, Patrick, Simon) 
 * @project (Semesterprojekt)
 * @version (2014.12.10)
 */ 
public class OrderUI
{
    private InputReader reader;
    private OrderCtr oCtr;

    /**
     * Constructor for objects of class OrderUI
     */
    public OrderUI()
    {
        reader = InputReader.getInstance();
        oCtr = new OrderCtr();
    }
    
    /**
     * Runs the OrderUI 
     */
    public void run(){
        printMenu();
        boolean finished = false;
        while(!finished) {
            String choise = reader.getInput();
            switch(choise){
                case "1":
                    createOrder();
                    break;
                case "2":
                    finished=true;
                    System.out.println("Farvel!");
                    break;
                default:
                    System.out.println("Ikke en kommando");
                    break;
            }
            printMenu();
        }
    }
    
    /**
     * Prints the Order menu to the terminal
     */
    private void printMenu(){
        System.out.println("**** Orderhåndtering ****");
        System.out.println("**  1. Opret ny order  **");
        System.out.println("**       2. Afslut     **");
        System.out.println("*************************");
        System.out.println(" ");
    }
    
    /**
     * Prints the addProductMenu to the terminal
     */
    private void printAddProductMenu(){
        System.out.println("1. Tilføj vare til order");
        System.out.println("2. Afslut order");
    }
    
    /**
     * Steps to create an order
     */
    private void createOrder(){
        //Employee information - already logged in 
        Employee em = new Employee("Jørgen Jensen", "Nørrevang 3", "J@J.dk", "88888888", 1);
        
        Customer co =  findCustomer();
        
        // create a new order
        Order o = oCtr.createOrder(co, em);
        
        printAddProductMenu();
        boolean finished = false;
        while(!finished) {
            String choise = reader.getInput();
            switch(choise){
                case "1":
                    Product p = findProduct();
                    printAddProductMenu();
                    String addToOrdre = reader.getInputWithLabel("Tilføj? (ja, nej)");
                    if(addToOrdre.equals("ja")){
                        String amount = reader.getInputWithLabel("Antal");
                        int amt = Integer.parseInt(amount);
                        // add to ordre and print subtotal
                        System.out.println("Subtotal: " + addProduct(o, p, amt));
                    }
                    printAddProductMenu();
                    break;
                case "2":
                    finished=true;
                    break;
                default:
                    System.out.println("Ikke en kommando");
                    break;
            }
        }
        
        oCtr.endOrder(o);
        System.out.println("Ordren blev oprettet!");
        printOrderInfo(o);
    }
    
    /**
     * Collects a customer phone and gets the customer from the controller
     */
    private Customer findCustomer(){
        
        Customer co = null;
        // Find customer in the system
        String customerPhone = reader.getInputWithLabel("Kundens telefonnr.");
        try{
        co = oCtr.findCustomer(customerPhone);
        //Print customer info
        printCustomerInfo(co); 
        }catch(Exception e){ 
          System.out.println("Fejl, kunden blev ikke fundet. Prøv igen");
          co = findCustomer();
        } 
        return co;
    }
    
    /**
     * Collects a product ID from the terminal and gets a product object back from the controller
     */
    private Product findProduct(){
        Product p = null;
        
        String productID = reader.getInputWithLabel("Produkt id.");
        // convert String to int
        int ID = Integer.parseInt(productID);
        
        try{
            p =  oCtr.findProduct(ID);
            printProductInfo(p);
        }catch(Exception e){
             System.out.println("Fejl, produktet  blev ikke fundet. Prøv igen");
             p = findProduct();
        }
        
        return p;
    }

    /**
     * Add´s a product to the order and return the subtotal
     * @param o, the order the product is to be added to
     * @params p, the product which is to be added to the order
     * @params amt, the amount of product to be added to the order
     */
    private double addProduct(Order o, Product p, int amt){
        return oCtr.addProduct(o, p, amt);
    }
    
    /**
     * Prints information abount a product
     * @params p, the product to be printet
     */
    private void printProductInfo(Product p){
        System.out.println("__________PRODUKTINFORMATION__________");
        System.out.println("Produkt ID " + p.getID());
        System.out.println("Produkt navn " + p.getName());
        System.out.println("Produktinformation " + p.getInfo());
        System.out.println("Produkt pris " + p.getPrice());
        System.out.println("Antal på lager " + p.getStock());
        System.out.println("Placering: " + p.getLocation());
        System.out.println("______________________________________");
    }
    
    /**
     * Prints information about a customer
     * @
     */
    
    private void printCustomerInfo(Customer co){ 
        System.out.println("-------------------------------------");
        System.out.println("Navn: " + co.getName() + " (Kundenr." + co.getCustomerID() + ")");
        System.out.println("Adresse: " + co.getAdress());
        System.out.println("Email: " + co.getEmail());
        System.out.println("Tlf. " + co.getPhone());
        System.out.println("-------------------------------------");
        System.out.println(" ");
        
    }
    
    private void printOrderInfo(Order o){
        System.out.println("__________________ORDRE_____________________");
        System.out.println("Order ID " + o.getID());
        System.out.println("Dato " + o.getDate());
        System.out.println("Du blev betjent af " + o.getEmployee().getName());
        System.out.println(" ");
        printCustomerInfo(o.getCustomer());
        System.out.println(" ");
        System.out.println("Produkter: ");
        
        ArrayList<PartOrder> partOrders = new ArrayList<PartOrder>();
        partOrders = o.getPartOrders();
     
        for(PartOrder partOrder : partOrders){
            Product p = partOrder.getProduct();
            System.out.println("--------------------------------");
            System.out.println("Produkt ID " + p.getID());
            System.out.println("Produkt navn " + p.getName());
            System.out.println("Produkt pris " + p.getPrice());
            System.out.println("Antal " + partOrder.getAmount());
            System.out.println("--------------------------------");
        }
        
        System.out.println(" ");
        System.out.println(" ");
        System.out.println("Total: " + o.getTotal());
        System.out.println("________________________");
        System.out.println("Total med rabat: " + o.getTotalWithDiscount());
        System.out.println(" ");
        System.out.println("____________________________________________");
    }
}
